#______________________________________________________________________________________________________________________________________________________________
#______________________________________________________________________________________________________________________________________________________________
#____________  FILE NAME: 2021_BushSchool_LegislativeCapstone_RegressionAnalysis.R  __________________________________________________________________________________________
#____________                                                                       __________________________________________________________________________________________
#____________  PURPOSE:  This R script reads in the total number of bills referred  __________________________________________________________________________________________
#____________             to committees in the 80th, 81st, 82nd, 83rd, 84th, 85th,  __________________________________________________________________________________________
#____________             86th, and 87th  Texas Legislative Session. We then        __________________________________________________________________________________________
#____________             conduct regression modeling using the variables included. __________________________________________________________________________________________
#____________                                                                       __________________________________________________________________________________________
#____________  AUTHORS:  Joel Chavez, Jorge Borrego                                 __________________________________________________________________________________________
#____________                                                                       __________________________________________________________________________________________
#____________  AFFILIATION: Dept. of Public Service and Admin.                      __________________________________________________________________________________________
#____________               The Bush School of Govt. and Public Service             __________________________________________________________________________________________
#____________               Texas A&M University                                    __________________________________________________________________________________________
#____________                                                                       __________________________________________________________________________________________
#____________  CONTACT: joel.chavez@tamu.edu | jab915@tamu.edu                      __________________________________________________________________________________________
#____________                                                                       __________________________________________________________________________________________
#____________  CREATED:  03.01.2021                                                 __________________________________________________________________________________________
#____________  LAST MODIFIED:  06.19.2021 (JC)                                      __________________________________________________________________________________________
#______________________________________________________________________________________________________________________________________________________________
#______________________________________________________________________________________________________________________________________________________________


#--This code reads in the our Spreadsheet from the Harvard Dataverse ----
all_sessions <- read.table("https://dataverse.harvard.edu/api/access/datafile/4809984", header = TRUE)

#________________________________________________________________________________________________________________________________________________________________________________________________________________________
#________________________________________________________________________________________________________________________________________________________________________________________________________________________

#--Regression Model 1 ----
#--This code runs a least-squares regression model with "PCT_Success_TotalReferred_PassedBoth" as the dependent variable
Regression_Model1_PCT_Success_TotalReferred_PassedBoth <- lm(PCT_Success_TotalReferred_PassedBoth ~ #--all variables after "~" are independent variables.
                                                               X80+X82+X83+X84+X85+X86+Recession_Session,
                                                             data=all_sessions)
#--This code displays the results from the model above
summary(Regression_Model1_PCT_Success_TotalReferred_PassedBoth)


#________________________________________________________________________________________________________________________________________________________________________________________________________________________
#________________________________________________________________________________________________________________________________________________________________________________________________________________________

#--Regression Model 2 ----
#--This code runs a least-squares regression model with "PCT_Success_TotalReferred_PassedBoth" as the dependent variable
Regression_Model2_PCT_Success_TotalReferred_PassedBoth <- lm(PCT_Success_TotalReferred_PassedBoth ~ #--all variables after "~" are independent variables.
                                                  X80+X82+X83+X84+X85+X86+Recession_Session+ #--main research variables
                                                  Senate+ 
                                                  Chair_Repub+ViceChair_Repub+
                                                  PCT_Repub+
                                                  Committee_Members,
                                                data=all_sessions)
#--This code displays the results from the model above
summary(Regression_Model2_PCT_Success_TotalReferred_PassedBoth)

#________________________________________________________________________________________________________________________________________________________________________________________________________________________
#________________________________________________________________________________________________________________________________________________________________________________________________________________________


#--Regression Model 3 ----
Regression_Model3_PCT_Success_TotalReferred_PassedBoth <- lm(PCT_Success_TotalReferred_PassedBoth ~ #--all variables after "~" are independent variables.
                                                  X80+X82+X83+X84+X85+X86+Recession_Session+ #--main research variables
                                                  Senate+ 
                                                  Chair_Repub+ViceChair_Repub+
                                                  PCT_Repub+
                                                  Committee_Members+
                                                  EconDev_Reg+Fiscal_Policy_Analysis+Gov_Ops_Employees+HHS+Higher_Educ+Justice_Public_Safety+Nat_Cultural_Resources+Technology+Transportation+Pub_Educ,
                                                data=all_sessions)
summary(Regression_Model3_PCT_Success_TotalReferred_PassedBoth)
#________________________________________________________________________________________________________________________________________________________________________________________________________________________
#________________________________________________________________________________________________________________________________________________________________________________________________________________________

#--This code creates the Heatmap in Figure 2 ----
library(ggplot2)
ggplot(all_sessions,aes(Session,PolicyArea,fill=PCT_Success_TotalReferred_PassedBoth))+
  geom_tile(color= "white",size=0.1)+ 
  scale_fill_gradient2(name="Committee Bill \n Sucess Rate",low="#3a4c6a",mid="#f2f2f2", high="darkred", midpoint=0.4)+
  scale_x_continuous(name="Texas Legislative Session")+
  scale_y_discrete(name="LBB Policy Area")+
  theme(legend.title.align=0.5,legend.box.just = "center")+
  theme_light()

#--This code provides a table of the three models ----
summary(Regression_Model1_PCT_Success_TotalReferred_PassedBoth)
summary(Regression_Model2_PCT_Success_TotalReferred_PassedBoth)
summary(Regression_Model3_PCT_Success_TotalReferred_PassedBoth)
